//
//  NSError+EV.m
//  EVClub
//
//  Created by sam on 14/11/19.
//  Copyright (c) 2014年 BitRice. All rights reserved.
//

#import "NSError+EV.h"
#import "NSDictionary+EV.h"

const NSInteger EVRevCodeAccoutExpired = 2;
const NSInteger EVRevCodeNotExist = 500;
const NSInteger EVRevCodeCollectSpotCheckStautsChanged = 601;
const NSInteger EVRevCodeSpotDeleted = 300000;
const NSInteger EVRevCodePayFailure = 600000;
const NSInteger EVRevCodePayException = 600001;
const NSInteger EVRevCodeRefundFailure = 600002;
const NSInteger EVRevCodeRefundException = 600003;
const NSInteger EVRevCodeRefundNotPayed = 600004;
const NSInteger EVRevCodeRefundCharging = 600005;
const NSInteger EVRevCodeRefundOddOrder = 600006;

@implementation NSError (EV)
const NSInteger EVRevCodeStartChargingFailByRefunding = 400002;
const NSInteger EVRevCodeStartChargingFailByNotPayDeposit = 400003;

+ (NSError  *)ev_error:(NSString *)errorMessage
{
    return [NSError ev_error:errorMessage revCode:kEVError_Code];
}

+ (NSError *)ev_error:(NSString *)errorMessage revCode:(NSInteger)code
{
    if (!errorMessage.length)
    {
        return [NSError errorWithDomain:@"充电网" code:kEVError_Code + code userInfo:@{kError_UserInfo:@"未知错误"}];
    }
    NSError *error = [NSError errorWithDomain:@"充电网" code:kEVError_Code + code userInfo:@{kError_UserInfo:errorMessage}];
    return error;
}

+ (NSError *)ev_errorWithResponse:(NSDictionary *)responseDictionary {
    if ([responseDictionary ev_errorCode] == 0) {
        return nil;
    }
    
    if ([responseDictionary isKindOfClass:[NSDictionary class]]) {
        return [self ev_error:[responseDictionary ev_errorMessage] revCode:[responseDictionary ev_errorCode]];
    }
    return [self ev_error:nil];
}

+ (NSError *)ev_networkError {
    return [NSError errorWithDomain:@"充电网" code:kCFURLErrorNotConnectedToInternet userInfo:nil];
}

- (NSInteger)ev_revCode {
    return self.code - kEVError_Code;
}

- (BOOL)isEvError
{
    if (self.code >= kEVError_Code)
    {
        return YES;
    }
    return NO;
}

- (NSString *)ev_errorMessage
{
    NSString *errorMessage = [self.userInfo ev_stringObjectForKey:kError_UserInfo];
    if (errorMessage.length)
    {
        return errorMessage;
    }
    switch (self.code) {
        case kCFURLErrorNotConnectedToInternet:
        case kCFURLErrorCannotConnectToHost:
        case kCFURLErrorNetworkConnectionLost:
        case kCFURLErrorCannotFindHost:
            errorMessage = @"网络不可用，请检查";
            break;
        case NSURLErrorTimedOut:
            errorMessage = @"通信超时，请检查网络是否正常";
            break;
        default:
            errorMessage = @"网络通信错误，请检查网络是否正常";
            break;
    }
    return errorMessage;
}

- (BOOL)isNetworkError {
    return self.code == kCFURLErrorCannotFindHost ||
            self.code == kCFURLErrorCannotConnectToHost ||
            self.code == kCFURLErrorNetworkConnectionLost ||
            self.code == kCFURLErrorNotConnectedToInternet ||
            self.code == kCFURLErrorTimedOut;
}

@end
